/*
 * copiarDirectorio.java
 *
 * Created on 27 de abril de 2007, 13:21
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package util;

import java.io.*;

/**
 *
 * @author Administrador
 */
public class CopiarDirectorio {
    
    public static void copiarDirectorio(File srcPath, File dstPath) throws IOException{
        if (srcPath.isDirectory()) {
            if (!dstPath.exists()) {
                dstPath.mkdir();
            }
            
            String files[] = srcPath.list();
            for(int i = 0; i < files.length; i++) {
                copiarDirectorio(new File(srcPath, files[i]), new File(dstPath, files[i]));
            }
        } else {
            if(!srcPath.exists()) {
                System.out.println("File or directory does not exist.");
                System.exit(0);
            } else {
                InputStream in = new FileInputStream(srcPath);
                OutputStream out = new FileOutputStream(dstPath);
                
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            }
        }
        System.out.println("Directory copied.");
    }
}
